package com.example.myapplication;

import static com.huawo.opensdk.ApplicationHolder.sApplication;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.huawo.opensdk.HwSifliDevice;
import com.huawo.opensdk.bean.WatchDialLayoutBean;
import com.huawo.opensdk.listener.OnConnectionStateListener;
import com.huawo.opensdk.listener.OnWatchFaceTransListener;
import com.huawo.qjs.QjsWidget;
import com.huawo.qjs.widgets.QjsBattery;
import com.huawo.qjs.widgets.QjsCalorie;
import com.huawo.qjs.widgets.QjsEmpty;
import com.huawo.qjs.widgets.QjsHeartrate;
import com.huawo.qjs.widgets.QjsHourHand;
import com.huawo.qjs.widgets.QjsMiniteHand;
import com.huawo.qjs.widgets.QjsSecondHand;
import com.huawo.qjs.widgets.QjsStep;
import com.huawo.qjs.widgets.QjsTime;
import com.huawo.qjs.widgets.QjsWeather;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    // request permissions callback code
    private static final int PERMISSION_REQUEST_CODE = 123;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // check permission
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)
                && PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN)
                && PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                && PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
            // request permissions
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.BLUETOOTH_SCAN,
                            Manifest.permission.BLUETOOTH_CONNECT,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE
                    },
                    PERMISSION_REQUEST_CODE);
        } else {

        }
        HwSifliDevice hwSifliDevice = new HwSifliDevice(getApplication());

        Button btConnect = findViewById(R.id.bt_connect);
        btConnect.setOnClickListener(view -> {
//            hwSifliDevice.connect("10:B1:05:01:00:38");
            hwSifliDevice.setOnConnectionStateListener(new OnConnectionStateListener() {
                @Override
                public void onStateChanged(@NonNull ConnectState state) {
                    if (state == ConnectState.STATE_CONNECTED) {
                        Log.e("MainActivity", "ConnectState.STATE_CONNECTED");
                    }

                    if (state == ConnectState.STATE_CONNECTING) {
                        Log.e("MainActivity", "ConnectState.STATE_CONNECTING");
                    }

                    if (state == ConnectState.STATE_DISCONNECTED) {
                        Log.e("MainActivity", "ConnectState.STATE_DISCONNECTED");
                    }
                }
            });
            hwSifliDevice.connect("10:A2:A4:05:74:A0");
            Log.e("MainActivity", "btConnect");
        });


        Button btDisconnect = findViewById(R.id.bt_disconnect);
        btDisconnect.setOnClickListener(view -> {
            hwSifliDevice.disconnect();
            Log.e("MainActivity", "btDisconnect");
        });


        Button button1 = findViewById(R.id.bt_online_watchface);
        button1.setOnClickListener(view -> {
            hwSifliDevice.sendDialBinFile(new File(sApplication.getCacheDir().toString() + File.separator + "JW_HS01WF001.zip"), new OnWatchFaceTransListener() {
                @Override
                public void onTransProgressStarting() {

                }

                @Override
                public void onTransProgressChanged(Integer progress) {

                }

                @Override
                public void onTransCompleted() {

                }

                @Override
                public void onTransError(Integer errorCode, String cause) {

                }

                @Override
                public void onInstallStateChange(Boolean isInstall) {

                }
            });
            Log.e("MainActivity", "button1");
        });

        Button button2 = findViewById(R.id.bt_diy_watchface);
        button2.setOnClickListener(view -> {
//            style   0, 1, 2, 3, 4 对应hawofit五种样式
            Integer style = WatchDialLayoutBean.WatchDialTimePosition.INSTANCE.getWATCH_FACE_TIME_1();
            ArrayList<QjsWidget> widgetList = new ArrayList<>();
            //setStyle  设置控件的样式  0 1 2 3

            //如果不显示样式则  new QjsEmpty("empty" + 1)添加
//            QjsEmpty qjsEmpty =new QjsEmpty("empty" + 1);
//            widgetList.add(step);
            if (style == WatchDialLayoutBean.WatchDialTimePosition.INSTANCE.getWATCH_FACE_TIME_0()) {
                QjsTime time = new QjsTime("time", 0xffffff, 480, 480);//添加时间控件
                time.setStyle(1);//设置控件的样式
                widgetList.add(time);
                QjsStep step = new QjsStep("step", 0xffffff);
                step.setStyle(0);
                widgetList.add(step);
                QjsCalorie qjsCalorie = new QjsCalorie("cal", 0xffffff);
                qjsCalorie.setStyle(0);
                widgetList.add(qjsCalorie);
                QjsHeartrate qjsHeartrate = new QjsHeartrate("hr", 0xffffff);
                qjsHeartrate.setStyle(0);
                widgetList.add(qjsHeartrate);
            }
            if (style == WatchDialLayoutBean.WatchDialTimePosition.INSTANCE.getWATCH_FACE_TIME_1()) {
                QjsTime time = new QjsTime("time", 0xffffff, 480, 480);//添加时间控件
                time.setStyle(0);//设置控件的样式
                widgetList.add(time);
                QjsStep step = new QjsStep("step", 0xffffff);
                step.setStyle(2);
                widgetList.add(step);
                QjsCalorie qjsCalorie = new QjsCalorie("cal", 0xffffff);
                qjsCalorie.setStyle(2);
                widgetList.add(qjsCalorie);
                QjsHeartrate qjsHeartrate = new QjsHeartrate("hr", 0xffffff);
                qjsHeartrate.setStyle(2);
                widgetList.add(qjsHeartrate);
            }
            if (style == WatchDialLayoutBean.WatchDialTimePosition.INSTANCE.getWATCH_FACE_TIME_2()) {
                QjsTime time = new QjsTime("time", 0xffffff, 480, 480);//添加时间控件
                time.setStyle(0);//设置控件的样式
                widgetList.add(time);
                QjsStep step = new QjsStep("step", 0xffffff);
                step.setStyle(2);
                widgetList.add(step);
                QjsCalorie qjsCalorie = new QjsCalorie("cal", 0xffffff);
                qjsCalorie.setStyle(2);
                widgetList.add(qjsCalorie);
                QjsHeartrate qjsHeartrate = new QjsHeartrate("hr", 0xffffff);
                qjsHeartrate.setStyle(2);
                widgetList.add(qjsHeartrate);
            }
            if (style == WatchDialLayoutBean.WatchDialTimePosition.INSTANCE.getWATCH_FACE_TIME_3()) {
                QjsTime time = new QjsTime("time", 0xffffff, 480, 480);//添加时间控件
                time.setStyle(0);//设置控件的样式
                widgetList.add(time);
                QjsWeather qjsWeather = new QjsWeather("weather", 0xffffff);
                qjsWeather.setStyle(0);
                widgetList.add(qjsWeather);
                QjsBattery battery = new QjsBattery("battery", 0xffffff);
                battery.setStyle(2);
                widgetList.add(battery);
                QjsHeartrate qjsHeartrate = new QjsHeartrate("hr", 0xffffff);
                qjsHeartrate.setStyle(2);
                widgetList.add(qjsHeartrate);
            }
            if (style == WatchDialLayoutBean.WatchDialTimePosition.INSTANCE.getWATCH_FACE_TIME_4()) {
                QjsBattery battery = new QjsBattery("battery", 0xffffff);
                battery.setStyle(1);
                widgetList.add(battery);
                QjsHeartrate qjsHeartrate = new QjsHeartrate("hr", 0xffffff);
                qjsHeartrate.setStyle(2);
                widgetList.add(qjsHeartrate);
                QjsStep step = new QjsStep("step", 0xffffff);
                step.setStyle(2);
                widgetList.add(step);
                QjsTime time = new QjsTime("time", 0xffffff, 480, 480);//添加时间控件
                time.setStyle(0);//设置控件的样式
                widgetList.add(time);
                widgetList.add(new QjsHourHand("hh", 0xffffff, 480, 480));
                widgetList.add(new QjsMiniteHand("mh", 0xffffff, 480, 480));
                widgetList.add(new QjsSecondHand("sh", 0xffffff, 480, 480));
            }

            hwSifliDevice.sendCustomWatchDialFile(new WatchDialLayoutBean(
                            5, style, 0, 0, 0xFF00FF, "", 480, 480, 235, 235, widgetList
                    ), sApplication.getCacheDir().toString() + File.separator + "20240613_142225target.gif",
                    new OnWatchFaceTransListener() {

                        @Override
                        public void onTransProgressStarting() {
                            Log.e("onTransProgressStarting", "onTransProgressStarting");
                        }

                        @Override
                        public void onTransProgressChanged(Integer progress) {
                            Log.e("onTransProgressChanged", "onTransProgressStarting");
                        }

                        @Override
                        public void onTransCompleted() {
                            Log.e("onTransCompleted", "onTransProgressStarting");
                        }

                        @Override
                        public void onTransError(Integer errorCode, String cause) {
                            Log.e("onTransError", "onTransProgressStarting");
                        }

                        @Override
                        public void onInstallStateChange(Boolean isInstall) {
                            Log.e("onInstallStateChange", "onTransProgressStarting");
                        }
                    });
            Log.e("MainActivity", "button2");
        });

        Button button3 = findViewById(R.id.bt_ota_firmware);
        button3.setOnClickListener(view -> {
//            hwSifliDevice.setOnFirmwareUpgradeListener(new OnFirmwareUpgradeListener() {
//                @Override
//                public void onFirmwareDownloadStarting() {
//
//                }
//
//                @Override
//                public void onFirmwareDownloadProgress(int progress) {
//
//                }
//
//                @Override
//                public void onFirmwareDownloadComplete() {
//
//                }
//
//                @Override
//                public void onUpgradeProgressStarting() {
//
//                }
//
//                @Override
//                public void onUpgradeProgressChanged(int progress) {
//
//                }
//
//                @Override
//                public void onUpgradeCompleted() {
//
//                }
//
//                @Override
//                public void onUpgradeAborted() {
//
//                }
//
//                @Override
//                public void onError(int code, @NonNull String cause) {
//
//                }
//            });
            hwSifliDevice.sendStartFirmwareUpgrade(
                    new File(sApplication.getCacheDir().toString() + File.separator + "HS01_LS20_V1.0.5B52.zip"),
                    new File(sApplication.getCacheDir().toString() + File.separator + "V1.0.5B50_to_V1.0.5B52_diff.zip"));
            Log.e("MainActivity", "button2");
        });

        Button button4 = findViewById(R.id.bt_diy_video_to_gif);
        button4.setOnClickListener(view -> {
            hwSifliDevice.videoToGif(this);
            Log.e("MainActivity", "button4");
        });

        Button button5 = findViewById(R.id.bt_ota_picture);
        button5.setOnClickListener(view -> {
            ArrayList<String> fileList = new ArrayList<>();
            fileList.add("/sdcard/Pictures/1700274321699.jpg");
            fileList.add("/sdcard/Pictures/1700274321699.jpg");
            ArrayList<Integer> fileNameList = new ArrayList<>();
            fileNameList.add(1);
            fileNameList.add(2);
            hwSifliDevice.sendStartPicture(this, fileList, fileNameList);
        });

        Button button6 = findViewById(R.id.bt_ota_music);
        button6.setOnClickListener(view -> {
            ArrayList<String> fileList = new ArrayList<>();
            fileList.add("/sdcard/找自己-陶喆.96.mp3");
            hwSifliDevice.sendStartMusic(this, fileList);
            Log.e("MainActivity", "button6");
        });

        Button button7 = findViewById(R.id.bt_ota_stop);
        button7.setOnClickListener(view -> {
            hwSifliDevice.sendAbortDialBinFile();
            Log.e("MainActivity", "button7");
        });
    }
}